package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.admin.entity.MaintainPlanContent;
import org.springblade.modules.admin.entity.MaintainPlanContentExt;
import org.springblade.modules.admin.entity.MaintainPlanDeferHistory;
import org.springblade.modules.admin.service.IMaintainPlanConfigService;
import org.springblade.modules.admin.service.IMaintainPlanContentExtService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.service.IMaintainPlanDeferHistoryService;
import org.springblade.modules.admin.vo.MaintainPlanDefertHistoryVO;
import org.springblade.modules.admin.vo.defert.PlanConfigVo;
import org.springblade.modules.admin.vo.defert.PlanDeferStatisticVo;
import org.springblade.modules.admin.wrapper.MaintainPlanDefertHistoryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/11/22 15:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/plan/defer/history")
@Api(value = "维保任务延期历史日志", tags = "维保任务延期历史日志接口")
public class MaintainPlanDeferHistoryController {

	private final IMaintainPlanDeferHistoryService maintainPlanDeferHistoryService;

	private final IMaintainPlanContentService maintainPlanContentService;
	private final IMaintainPlanContentExtService maintainPlanContentExtService;
	private final IMaintainPlanConfigService maintainPlanConfigService;

	/**
	 * 分页 维保任务延期历史日志
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入maintainPlanDefertHistory")
	public R<IPage<MaintainPlanDefertHistoryVO>> list(MaintainPlanDeferHistory maintainPlanDefertHistory, Query query) {
		query.setDescs("create_time");
		IPage<MaintainPlanDeferHistory> pages = maintainPlanDeferHistoryService.page(Condition.getPage(query), Condition.getQueryWrapper(maintainPlanDefertHistory));
		return R.data(MaintainPlanDefertHistoryWrapper.build().pageVO(pages));
	}

	@GetMapping("/getPlanDeferStatistic")
	@ApiOperation("获取工单延期统计")
	public R<PlanDeferStatisticVo> getPlanDeferStatistic() {
		return R.data(maintainPlanDeferHistoryService.getPlanDeferStatistic());
	}

	@GetMapping("/init")
	@ApiOperation("延期工单数据初始化")
	public R<String> init() {

		List<MaintainPlanDeferHistory> saveList = new ArrayList<>(15000);
		List<MaintainPlanContent> list = maintainPlanContentService.getList();
		final List<MaintainPlanContentExt> contentExtList = maintainPlanContentExtService.getList();
		final Map<Long, List<MaintainPlanContentExt>> listMap = contentExtList.stream().collect(Collectors.groupingBy(MaintainPlanContentExt::getPlanId));
		final List<Long> elevatorIds = list.stream().map(MaintainPlanContent::getElevatorId).distinct().collect(Collectors.toList());
		final List<PlanConfigVo> planConfigVos = maintainPlanConfigService.getPlanConfigVo(elevatorIds);
		final Map<Long, List<PlanConfigVo>> configMap = planConfigVos.stream().collect(Collectors.groupingBy(PlanConfigVo::getElevatorId));
		List<MaintainPlanContentExt> extList = new ArrayList<>(15000);
		for (int i = 0; i < list.size(); i++) {
			final MaintainPlanContent addPlan = list.get(i);
			MaintainPlanDeferHistory maintainPlanDefertHistory = new MaintainPlanDeferHistory();
			maintainPlanDefertHistory.setPlanId(addPlan.getId());
			maintainPlanDefertHistory.setPlanDate(addPlan.getPlanDate());
			MaintainPlanContentExt maintainPlanContentExt = listMap.get(addPlan.getId()).get(0);

			if (configMap.get(addPlan.getElevatorId()) == null || configMap.get(addPlan.getElevatorId()).size() == 0) {
				continue;
			}
			PlanConfigVo planConfigVo = configMap.get(addPlan.getElevatorId()).get(0);
			BeanUtils.copyProperties(planConfigVo, maintainPlanDefertHistory);
			maintainPlanDefertHistory.setPlanType(planConfigVo.getAuditType());
			maintainPlanDefertHistory.setCreateUserName(AuthUtil.getNickName());
			maintainPlanDefertHistory.setTenantId(addPlan.getTenantId());
			saveList.add(maintainPlanDefertHistory);

			maintainPlanContentExt.setAuditType(planConfigVo.getAuditType());
			extList.add(maintainPlanContentExt);
		}

		final List<List<MaintainPlanDeferHistory>> partition = Lists.partition(saveList, 30);
		for (List<MaintainPlanDeferHistory> list1 : partition) {
			maintainPlanDeferHistoryService.saveBatch(list1);
		}
		final List<List<MaintainPlanContentExt>> partition1 = Lists.partition(extList, 30);
		for (List<MaintainPlanContentExt> list1:partition1) {
			maintainPlanContentExtService.updateBatchById(list1);
		}
		return R.data("初始化数据成功");
	}

}
